package com.jie.servlet;

import com.jie.service.AgencyService;
import com.jie.service.RelationService;
import com.jie.service.UserService;
import com.jie.service.impl.AgencyServiceImpl;
import com.jie.service.impl.RelationServiceImpl;
import com.jie.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 12904 on 2018/8/2.
 */
@WebServlet(name = "deleteServlet")
public class delAgencyServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request,response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        int uid = Integer.parseInt(request.getParameter("uid"));
        AgencyService as = new AgencyServiceImpl();
        UserService us = new UserServiceImpl();
        RelationService rs = new RelationServiceImpl();

        int num = 0;                //标志受影响行数
        //首先删除user表数据
         num += us.delUser(uid);
        //然后删除agency表数据
         num += as.delAgency(as.findByuid(uid));
        //最后删除agency_relation表数据      bug : 先删除 agency表数据   agency_relation表删除不了  而且返回的是null
        //分情况处理  假如是省代理先判断该生代理下是否有市代理  市代理同理
        //如果没有代理  则不删除
          if (rs.findByAid(as.findByuid(uid),0) != null){    //如果agency_relation有数据  就删除
             num += rs.del(rs.findByAid(as.findByuid(uid),0).getArid());
          }
        //删除至少两条数据 则成功
        if ( num >= 2 ){
            out.print(num);
            out.flush();
            out.close();
        }

    }
}
